
import java.util.*;
import java.io.*;
import java.net.*;


public class LoadSite
{
    /**
     * lycospro.lycos.com 
    private static URL getPageURL(String q, int t) throws MalformedURLException {
	String queryURLString = 
	    "http://lycospro.lycos.com/srchpro/?type=websites&query=" + q + "&first=" + (t + 1);
	return new URL(queryURLString);
    }
    */


    /**
     * www.google.com
     */
    private static URL getPageURL(String q, int t) throws MalformedURLException {
	String queryURLString = 
	    "http://www.google.com/search?q=" + q + "java&start=" + (t + 1) + "&num=10";
	return new URL(queryURLString);
    }


    /** 
     * Quietly close the reader, ignoring IOExceptions.
     */
    private static void closeReader(Reader r) {
	if (r != null) {
	    try {
		r.close();
	    }
	    catch (IOException ignore) {
	    }
	}
    }

    public static void main(String[] args) 
    {
	System.getProperties().put("proxyHost", "webcache-cup");
	System.getProperties().put("proxyPort", "8080");

	String query = (args.length >= 1) ? args[0] : "java";
	char c[] = new char[1];

	long t0 = System.currentTimeMillis();
	for(int i = 0; i < 200; i+= 10) {
	    Reader inputReader = null;
	    try {
		InputStream ist = getPageURL(query, i).openStream();
		inputReader = new InputStreamReader(ist);
		while(inputReader.read(c, 0, 1) == 1) {
		    System.out.print(c[0]);
		}
	    }
	    catch (Exception e) {
		e.printStackTrace();
	    }
	    finally {
		closeReader(inputReader);
	    }
	}
	long t1 = System.currentTimeMillis();

	int nSeconds = (int)((double)(t1 - t0) / 1000.0);
	System.out.println("\n\nElapsed Time: " + nSeconds + " seconds");
    }
}
